package Tools;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map.Entry;

public class Graph implements Serializable{
	
	HashMap<Integer, Node> map;
	
	public Graph() {
		map = new HashMap<Integer, Node>();
		map.put(3000, new Node(3,0,0,0));
		map.put(300, new Node(0,3,0,0));
		map.put(30, new Node(0,0,3,0));
		map.put(3, new Node(0,0,0,3));
		createGraph();
	}
	
	public void createGraph(){
		createGraph(new Node());
		
	}
	
	public void createGraph(Node node){
		ArrayList<Node> links = new ArrayList<Node>(); 
		node = node.clone();
		
		//if(node.isValid())
			map.put(node.hashCode(), node);
			
		for(int i = 0; i < 4; i++){
			if(node.get(i) == 0){
				
				node.set(i, 1);
				createGraph(node);
				links.add(node.clone());
				
				node.set(i, 0);

				if(!node.contains2()){

					node.set(i, 2);
					createGraph(node);
					links.add(node.clone());
					
					node.set(i, 0);
				}
			}
		}
		node.links = links;
	}
	
	public void printNodes(){
		for(Entry<Integer, Node> e : map.entrySet()){
			StringBuilder sb = new StringBuilder();
			for(Node n : e.getValue().links)
				sb.append(n+" ");
			System.out.println(e.getValue()+" | "+sb.toString());
		}
	}
	
	public HashSet<Node> getNodes(Node n, HashSet<Node> nodes){
		if(n!=null){
			if(n.isValid())
				nodes.add(n);
			
			for(Node goTo : n.links){			
				getNodes(map.get(goTo.hashCode()), nodes);
			}
		}else{
			System.out.println("Some null value!");
		}
		return nodes;
	}
	
	public ArrayList<int[]> getPossibleOpinions(int[] opinion){
		ArrayList<int[]> list = new ArrayList<int[]>();
		
		
		
		System.out.println(" === ");
		for(int i=0;i<opinion.length;i++)
			System.out.print(opinion[i]+" ");
		System.out.println();
		for(Node n : getNodes( map.get(new Node(opinion).hashCode()), new HashSet<Node>())){
			System.out.println(n);
			list.add(n.key);
		}
		
		return list;
	}
}
